package com.adopapa.nezhacloud.register.service;

import java.time.Instant;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.adopapa.nezhacloud.ddd.service.BaseService;
import com.adopapa.nezhacloud.redis.RedisService;
import com.adopapa.nezhacloud.register.conf.RabbitConfig;
import com.adopapa.nezhacloud.register.conf.RegisterConstants;
import com.adopapa.nezhacloud.register.domain.StudentInfo;
import com.rabbitmq.client.Channel;

@Component
@RabbitListener(queues = RabbitConfig.QUEUE)
public class DataReceiver extends BaseService {

	@Autowired
	private RedisService redisService;
	@Autowired
	private StudentInfoService studentInfoService;

	@RabbitHandler
	public void process(String data, Channel channel, Message message) {
		System.out.println(data);
		System.out.println(message);
		try {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

		} catch (Exception e) {
			logger.info(ExceptionUtils.getMessage(e));
		}
	}

	@SuppressWarnings("rawtypes")
	@RabbitHandler
	public void process(Map data, Channel channel, Message message) {
		logger.info("接收的消息为: {}", message.toString());
		try {

			String name = (String) data.get("name");
			String cellphone = (String) data.get("cellphone");
			String career = (String) data.get("career");
			String province = (String) data.get("province");
			String city = (String) data.get("city");
			String town = (String) data.get("town");
			String doulin = (String) data.get("doulin");
			 
			Long fans = (Long) data.get("fans");

			logger.info("信息接收: {}", data);

			StudentInfo studentInfo = new StudentInfo();
			studentInfo.setName(name);
			studentInfo.setCellphone(cellphone);
			studentInfo.setCareer(career);
			studentInfo.setProvince(province);
			studentInfo.setCity(city);
			studentInfo.setTown(town);
			studentInfo.setDoulin(doulin);
			studentInfo.setFans(fans);
			studentInfo.setCreateTime(Instant.now());
			studentInfo.setUpdateTime(Instant.now());
			
			logger.info("保存到数据库");
			studentInfoService.saveStudentInfo(studentInfo);
			
			logger.info("保存到缓存");
			RedisTemplate<String, Object> redisTemplate = //
					redisService.getRedisTemplate(RegisterConstants.REGISTER_REDIS_DBINDEX);
			redisTemplate.opsForHash().put("student", studentInfo.getCellphone(), studentInfo.getName());

			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

		} catch (Exception e) {
			logger.info(ExceptionUtils.getStackTrace(e));
			// channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		}
	}

//	public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
//		String result = "#{\"code\":1,\"messages\":[\"请求成功\"],\"result\":null}#_#_";
//		String[] parts = result.split("#");
//		for (String string : parts) {
//			System.out.println(string);
//		}
//		JsonNode jsonNode = JsonUtil.objectMapper.readTree(parts[1]);
//		System.out.println(jsonNode.get("code").asText());
//
//		System.err.println(result.replaceAll("\\\\", ""));
//
//	}

}
